#! /bin/sh
#**********************************************************
#* Author        : aliceblue
#* Email         : pryoyi@gmail.com
#* Last modified : 2020-3-17 09:14:42
#* Filename      : build
#* Description   : 
#* *******************************************************
 
 
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)

targetProp=$script_dir/"indolent.properties"

file_ele=("backup.sh" "restart.sh" "shutdown.sh" "showlog.sh" "startup.sh")
log_location=$script_dir/"log"
backup_location=$script_dir/"backup"
jar_name=
VM_param="-Xms1024m -Xmx2048m"





flag=''

# 判断是否覆盖
function is_override() {
    echo -n "是否覆盖之前的执行脚本(y/n):"
    while IFS= read -r -s -n1 char
    do
        # 如果读入的字符为空，则退出 while 循环
        if [ -z $char ]
        then
            echo
            break
        fi
        # 如果输入的是退格或删除键，则移除一个字符
        if [[ $char == $'\x08' || $char == $'\x7f' ]]
        then
            [[ -n $flag ]] && flag=${flag:0:${#flag}-1}
            printf '\b \b'
        else
            flag+=$char
            printf $char
        fi
    done
}



# 获取properties预设的值
function get_prop(){
    grep "${1}" $targetProp | grep '^[^#]' | cut -d'=' -f 2- | sed 's/\r//'
}

# 构建脚本
function build_shell() {

	# 构建文件夹
	echo "开始构建......"
	rm -rf $script_dir/autoScript
	mkdir -p $script_dir/autoScript
	if [[ ! -d $log_location ]]
	then
		mkdir $log_location
	fi
	if [[ ! -d $backup_location ]]
	then
		mkdir $backup_location
	fi
	
	# 构建startup.sh
	rm -rf $script_dir/autoScript/startup.sh && touch $script_dir/autoScript/startup.sh
	echo "jarName=$jar_name" >> $script_dir/autoScript/startup.sh
	echo "var=\`date +%Y%m%d\`" >> $script_dir/autoScript/startup.sh
	echo "nohup java -jar $script_dir/\$jarName $VM_param >$log_location/log-\$var.out 2>&1 &" >> $script_dir/autoScript/startup.sh
	echo "startup.sh......"
	
	# 构建shutdown.sh
	rm -rf $script_dir/autoScript/shutdown.sh && touch $script_dir/autoScript/shutdown.sh
	echo "jarName=$jar_name" >> $script_dir/autoScript/shutdown.sh
	echo "PID=\`ps -ef|grep \$jarName |grep -v grep|awk '{print \$2}' \`" >> $script_dir/autoScript/shutdown.sh
	echo "if [ -n \"\$PID\" ]; then" >> $script_dir/autoScript/shutdown.sh
	echo "    kill -9 \$PID" >> $script_dir/autoScript/shutdown.sh
	echo "    echo \"结束\$PID进程\"" >> $script_dir/autoScript/shutdown.sh
	echo "else" >> $script_dir/autoScript/shutdown.sh
	echo "    echo \"\$jarName未启动\"" >> $script_dir/autoScript/shutdown.sh
	echo "fi" >> $script_dir/autoScript/shutdown.sh
	echo "shutdown.sh......"
	
	# 构建restart.sh
	rm -rf $script_dir/autoScript/restart.sh && touch $script_dir/autoScript/restart.sh
	echo "jarName=$jar_name" >> $script_dir/autoScript/restart.sh
	echo "PID=\`ps -ef|grep \$jarName |grep -v grep|awk '{print \$2}' \`" >> $script_dir/autoScript/restart.sh
	echo "if [ -n \"\$PID\" ]; then" >> $script_dir/autoScript/restart.sh
	echo "    kill -9 \$PID" >> $script_dir/autoScript/restart.sh
	echo "    echo \"结束\$PID进程\"" >> $script_dir/autoScript/restart.sh
	echo "fi" >> $script_dir/autoScript/restart.sh
	echo "var=\`date +%Y%m%d\`" >> $script_dir/autoScript/restart.sh
	echo "nohup java -jar $script_dir/\$jarName $VM_param >$log_location/log-\$var.out 2>&1 &" >> $script_dir/autoScript/restart.sh
	echo "restart.sh......"
	
	# 构建backup.sh
	rm -rf $script_dir/autoScript/backup.sh && touch $script_dir/autoScript/backup.sh
	echo "jarName=$jar_name" >> $script_dir/autoScript/backup.sh
	echo "times=\`date +%s\`" >> $script_dir/autoScript/backup.sh
	echo "oldJarName=\$times-\$jarName" >> $script_dir/autoScript/backup.sh
	echo "if [[ -f "\"$script_dir/\$jarName\"" ]];then" >> $script_dir/autoScript/backup.sh
	echo "    cp $script_dir/\$jarName $backup_location/\$oldJarName" >> $script_dir/autoScript/backup.sh
	echo "fi" >> $script_dir/autoScript/backup.sh
	echo "backup.sh......"
	
	# 构建showlog.sh
	rm -rf $script_dir/autoScript/showlog.sh && touch $script_dir/autoScript/showlog.sh
	echo "last_log_name=\$(ls -lt $log_location | grep log-20 | head -n 1 | awk '{print \$9}')" >> $script_dir/autoScript/showlog.sh
	echo "if [[ -f "\"$log_location/\$last_log_name\"" ]];then" >> $script_dir/autoScript/showlog.sh
	echo "    tail -f -n 60 $log_location/\$last_log_name" >> $script_dir/autoScript/showlog.sh
	echo "else" >> $script_dir/autoScript/showlog.sh
	echo "    echo \"不存在日志文件\"" >> $script_dir/autoScript/showlog.sh
	echo "fi" >> $script_dir/autoScript/showlog.sh
	echo "showlog.sh......"
	
	
	echo
	echo "构建完成！"
}

function init() {
	# 获取初始化参数
	if [[ ! -z $(get_prop jarName) ]]
	then
		jar_name=$(get_prop jarName)
	fi
	if [[ ! -z $(get_prop VMparam) ]]
	then
		VM_param=$(get_prop VMparam)
	fi
	if [[ ! -z $(get_prop logLocation) ]]
	then
		log_location=$(get_prop logLocation)
	fi
	if [[ ! -z $(get_prop backupLocation) ]]
	then
		backup_location=$(get_prop backupLocation)
	fi

	# 获取jar
	if [ -z "$jar_name" ]
	then

		jar_count=0
		for filename in `ls`
		do
			if [ "jar" == "${filename##*.}" ]
			then
				let jar_count++
				jar_name=$filename
			fi
		done

		if [ $jar_count -eq 1 ]
		then
			echo "识别到jar包"
			echo
		elif [ $jar_count -eq 0 ]
		then
			echo "未识别到jar包，生成失败"
			exit
		else
			echo "识别到多个jar，jar必须唯一...."
			exit
		fi

	fi
	
	# 输出参数
	echo "jar："$jar_name
	echo "虚拟机参数："$VM_param
	echo "日志输出位置："$log_location
	echo "jar包备份位置："$backup_location
	echo
	
	build_shell
}




# 是否是首次生成，是否覆盖
if [[ -d $script_dir/'autoScript' ]]
then
	is_override
	
    #判断输入
    if [ $flag == "y" ]
    then
		rm -rf $script_dir/autoScript && mkdir $script_dir/autoScript
		echo "重新生成脚本中......"
		echo
		init
    else
        echo '====执行被取消===='
		exit
    fi

else	#首次生成，直接初始化并创建
	init
fi


